package com.jrx.anytxn.transaction.job.paph.step;

import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.service.IPaphFileService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TranAcctDetailWriter implements ItemWriter<PaphFileBean> {

    @Autowired
    private PaphFileConfig paphFileConfig;
    @Autowired
    private IPaphFileService iPaphFileService;
    @Override
    public void write(List<? extends PaphFileBean> items) throws Exception {
        if (CollectionUtils.isNotEmpty(items)) {
            Map<String, List<PaphFileBean>> map = items.stream().collect(Collectors.groupingBy(PaphFileBean::getProductType));
            map.forEach(
                    (productType, list) -> {
                        List<String> messageList = list.stream().map(PaphFileBean::getFileString).collect(Collectors.toList());
                        iPaphFileService.generateFile(messageList, paphFileConfig.getTraAcctDetailFilePath(),
                                TransBizConstant.TRA_ACCT_DETAIL, paphFileConfig.getFileType(), productType);
                    }
            );

        }
    }
}
